package com.angel.service.ServiceImpl;

import com.angel.bean.*;
import com.angel.bean.dto.VipInfoDto;
import com.angel.mapper.BranchesDao;
import com.angel.mapper.UserInfoDao;
import com.angel.mapper.VipCardDao;
import com.angel.service.BranchesService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.angel.mapper.VipInfoDao;
import com.angel.service.VipInfoService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

@Service
public class VipInfoServiceImpl implements VipInfoService {

    @Resource
    private VipInfoDao vipInfoDao;
    @Resource
    private BranchesDao branchesDao;
    @Resource
    private VipCardDao vipCardDao;
    @Resource
    private UserInfoDao userInfoDao;

    @Override
    public int insert(VipInfo vipInfo) {
        return vipInfoDao.insert(vipInfo);
    }

    @Override
    public int insertSelective(VipInfo vipInfo) {
        return vipInfoDao.insertSelective(vipInfo);
    }

    @Override
    public int insertList(List<VipInfo> vipInfos) {
        return vipInfoDao.insertList(vipInfos);
    }

    @Override
    public int update(VipInfo vipInfo) {
        return vipInfoDao.update(vipInfo);
    }

    @Override
    public int updateVipNo(VipInfo vipInfo) {
        return vipInfoDao.updateVipNo(vipInfo);
    }

    @Override
    public List<VipInfo> findAll(VipInfo vipInfo) {
        return vipInfoDao.findAll(vipInfo);
    }

    @Override
    public VipInfo findOne(VipInfo vipInfo) {
        List<VipInfo> list = this.findAll(vipInfo);
        if (list == null || list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public List<VipInfoDto> findAllHaveCardName(VipInfo vipInfo) {
        return vipInfoDao.findAllHaveCardName(vipInfo);
    }

    @Override
    public VipInfoDto findOneDto(VipInfo vipInfo) {
        List<VipInfoDto> list = this.findAllHaveCardName(vipInfo);
        if (list == null || list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public List<VipInfo> searchVipInfo(VipInfo vipInfo) {
        return vipInfoDao.searchVipInfo(vipInfo);
    }

    @Override
    public int delVipInfo(VipInfo vipInfo) {
        return vipInfoDao.delVipInfo(vipInfo);
    }

    /**
     * 充值逻辑处理
     *
     * @param vipInfo
     * @param employee
     * @return
     */
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public Map recharge(VipInfo vipInfo, Employee employee, int sumCharge, int count) throws Exception {
        Map result = new HashMap();
        result.put("success", false);
        Branches branches = new Branches();
        branches.setBranId(employee.getBranchesId());
        branches = branchesDao.findOneBranId(branches);
        if (branches == null) {
            return result;
        } else {
            vipInfo.setChargeDtae(new Timestamp(System.currentTimeMillis()));
            //会员卡剩余的余额
            VipCard vipCard = new VipCard();
            vipCard.setVipId(vipInfo.getVipId());
            vipCard = vipCardDao.findVipCard(vipCard).get(0);

            String bb = vipInfo.getBalance() == null ? "0" : vipInfo.getBalance();
            double remindMoney = Double.valueOf(bb);
            if (vipCard.getVipLevel() == 0) {
                //体验卡充值次数
                int counttime = vipInfo.getRemainTime() == null ? 0 : vipInfo.getRemainTime();
                vipInfo.setRemainTime(counttime + count);
            } else {
                //付费卡充值金额
                remindMoney = remindMoney + sumCharge;
                vipInfo.setBalance(String.valueOf(remindMoney));
            }

            //更新总店充值余额
            Integer sum = branches.getReserve15() == null ? 0 : branches.getReserve15();
            int s = sum + sumCharge;
            branches.setReserve15(s);
            UserInfo userInfo = new UserInfo();
            userInfo.setId(vipInfo.getUserId());
            userInfo = userInfoDao.findAll(userInfo).get(0);
            if (userInfo.getSumCharge() == null || userInfo.getSumCharge() ==""){
                userInfo.setSumCharge(String.valueOf(sumCharge));
            }else {
               int tt = Integer.valueOf(userInfo.getSumCharge());
               userInfo.setSumCharge(String.valueOf(tt + sumCharge));
            }
            userInfoDao.update(userInfo);

            int re = branchesDao.update(branches);

            int re2 = vipInfoDao.update(vipInfo);
            if (re == 0) {
                result.put("msg", "分店充值金额更新失败");
                Exception e = new Exception("处理失败");
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                throw e;
            }
            if (re2 == 0) {
                result.put("msg", "会员余额更新失败");
                Exception e = new Exception("处理失败");
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                throw e;
            }
            if (re != 0 && re2 != 0) {
                result.put("success", true);
                result.put("msg", "充值成功，充值的金额是："+sumCharge);
            }
        }
        return result;
    }


}
